export type Channel = {
    id: number
    name: string
}

export type MoreAction = {
    visible: boolean
    articleId: string
    channelId: number
}

export type Article = {
    art_id: string
    title: string
    aut_id: string
    aut_name: string
    comm_count: string
    pubdate: string
    cover: {
        type: string
        image: string[]
    }
}

export type Articles = {
    [index: number]: {
        timestamp: string
        list: Article[]
    }
}

type HomeType = {
    userChannels: Channel[]
    allChannels: Channel[]
    articles: Articles
    moreAction: MoreAction

}

export type ArticlePayload = {
    channelId: number
    timestamp: string
    list: Article[]
}

const initValue: HomeType = {
    userChannels: [],
    allChannels: [],
    // 存储所有的文章列表
    articles: {},
    moreAction: {
        visible: false,
        articleId: '',
        channelId: -1
    }
} as HomeType

export type HomeAction = {
    type: 'home/saveChannels'
    payload: Channel[]
} | {
    type: 'home/saveAllChannels'
    payload: Channel[]
} | {
    type: 'home/saveArticleList'
    payload: ArticlePayload
} | {
    type: 'home/saveMoreArticleList'
    payload: ArticlePayload
} | {
    type: 'home/setMoreAction'
    payload: MoreAction
}

export default function reducer(state = initValue, action: HomeAction) {
    switch (action.type) {
        case 'home/saveChannels':
            return {
                ...state,
                userChannels: action.payload
            }

        case 'home/saveAllChannels':
            return {
                ...state,
                allChannels: action.payload
            }

        case 'home/saveArticleList':
            const { list, timestamp, channelId } = action.payload

            return {
                ...state,
                articles: {
                    ...state.articles,
                    [channelId]: {
                        timestamp: timestamp,
                        // 如果是loadMore，追加数据否则，覆盖数据
                        list: list
                    }
                }
            }

        case 'home/saveMoreArticleList':
            return {
                ...state,
                articles: {
                    ...state.articles,
                    [action.payload.channelId]: {
                        timestamp: action.payload.timestamp,
                        list: [...state.articles[action.payload.channelId].list, ...action.payload.list]
                    }
                }
            }

        case 'home/setMoreAction':
            return {
                ...state,
                moreAction: action.payload
            }

        default:
            return state
    }
}